package wechat.subject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.BaseModel;
import com.driver.SpecialConvert;
import com.filter.BaseController;

import beans.subject.SpecialBean;
import beans.subject.subchapterBean;
import beans.subject.subinfo;
import beans.subject.subsetquestion;
import beans.sys.UserBean;
import beans.user.suberror;
import beans.user.subresult;
import beans.user.user;
import beans.wxresult.Data;
import beans.wxresult.Rec;
import beans.wxresult.WXResult;
import service.subject.SubChapterServiceImpl;
import service.subject.SubInfoServiceImpl;
import service.subject.SubSetQuestionImpl;
import service.subject.SubSpecialServiceImpl;
import service.user.SubErrorServiceImpl;
import service.user.SubResultServiceImpl;

@Controller
@RequestMapping("/wxsubject")
public class CSubjectController extends BaseController{

	@Resource
	private SubSetQuestionImpl<subsetquestion> _SubSetQuestionImpl;

	@Resource
	private SubInfoServiceImpl<subinfo> subInfoServiceImpl;

	@Resource
	private SubChapterServiceImpl<subchapterBean> _SubChapterServiceImpl;
	@Resource
	private SubSpecialServiceImpl<SpecialBean> _SubSpecialServiceImpl;

	@Resource
	private SubResultServiceImpl<subresult> subResultServiceImpl;

	@Resource
	private SubErrorServiceImpl<suberror> subErrorServiceImpl;
	/**
	 * 免费试题
	 * @return
	 */
	@RequestMapping("/free")
	public String Free() {
		return "wechat/subject/free";
	}

	@RequestMapping("/subjectinfo")
	@ResponseBody
	public void SubjectInfo(String exam,String examtype,String infotype,String id) {
		WXResult wxResult = new WXResult();
		try {
			wxResult.setStatus("1");
			if(infotype.equals("free")) {//免费试题
				List<subinfo> list = subInfoServiceImpl.getSubjectInfo("free");
				Data data = new Data();
				data.setCount(list.size());
				data.setErr(0);
				data.setFileurl("");
				data.setTitle("免费试题");
				List<Rec> recList = new ArrayList<>();
				for(int i=0,j=list.size();i<j;i++) {
					Rec rec= new Rec(list.get(i),i+1);
					recList.add(rec);
				}
				data.setRec(recList);
				wxResult.setData(data);
			}
		}
		catch(Exception e) {
			wxResult.setStatus("0");
			e.printStackTrace();
		}
		responseJson(wxResult);
	}

	@RequestMapping("/errorsub")
	@ResponseBody
	public void errorsub(subresult result) {
		Map<String,String> m = new HashMap<>();
		m.put("url", "/wx/index");
		responseJson(m);
	}

	@RequestMapping("/errorsub2")
	@ResponseBody
	public void errorsub2(subresult result) {
		Map<String,String> m = new HashMap<>();

		user u = (user)getReq().getSession().getAttribute("wxuser");
		try {
			String id = UUID.randomUUID().toString();
			Date now = new Date();
			result.setId(id);
			result.setAddtime(now);
			result.setUid(u.getId());
			subResultServiceImpl.add(result);

			List<suberror> errorList = new ArrayList<>();
			String[] errlist = getReq().getParameterValues("errlist[]");
			if(errlist!=null) {
				for(String s : errlist) {
					suberror error = new suberror();
					error.setId(UUID.randomUUID().toString());
					error.setAddtime(now);
					error.setExam(result.getExam());
					error.setExamtype(result.getExamtype());
					error.setSrid(id);
					error.setSubid(s);
					error.setUid(u.getId());
					errorList.add(error);
				}
				if(errorList.size()>0)
					subErrorServiceImpl.Adds(errorList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		m.put("url", "/wx/index");
		responseJson(m);
	}

	/**
	 * 科目信息
	 * @param drivertype
	 * @return
	 */
	@RequestMapping("/kemu")
	public String Kemu(String drivertype) {
		getReq().setAttribute("drivertype", drivertype);

		List<subresult> resultList = subResultServiceImpl.getResult(10);
		for(subresult result : resultList) {
			result.setUname(result.getUname().substring(0,result.getUname().length()-1)+"*");
		}

		getReq().setAttribute("resultList", resultList);

		return "wechat/subject/kemu";
	}

	/**
	 * 章节信息
	 * @param drivertype
	 * @return
	 */
	@RequestMapping("/zhangjie")
	public String zhangjie(String drivertype,String subtype) {
		getReq().setAttribute("drivertype", drivertype);
		getReq().setAttribute("subtype", subtype);
		List<subchapterBean> subchapterList = _SubChapterServiceImpl.getByDriverAndSub(drivertype, SpecialConvert.SpecialConvertInfo(subtype));

		getReq().setAttribute("subchapterList",subchapterList);

		return "wechat/subject/zhangjie";
	}

	/**
	 * 专项训练
	 * @param drivertype
	 * @return
	 */
	@RequestMapping("/zhuangxiang")
	public String zhuangxiang(String drivertype,String subtype) {
		getReq().setAttribute("drivertype", drivertype);
		getReq().setAttribute("subtype", SpecialConvert.SpecialConvertInfo(subtype));

		List<SpecialBean> specialList = _SubSpecialServiceImpl.getByDriverAndSub(drivertype, SpecialConvert.SpecialConvertInfo(subtype));

		getReq().setAttribute("specialList", specialList);
		return "wechat/subject/zhuangxiang";
	}

	/**
	 * 强化
	 * @param drivertype
	 * @param subtype
	 * @return
	 */
	@RequestMapping("/qianhua")
	public String qianhua(String drivertype,String subtype) {
		getReq().setAttribute("drivertype", drivertype);
		getReq().setAttribute("subtype", SpecialConvert.SpecialConvertInfo(subtype));

		List<subsetquestion> subsetList = _SubSetQuestionImpl.getByDriverSubType(drivertype,SpecialConvert.SpecialConvertInfo(subtype));
		getReq().setAttribute("subsetList", subsetList);

		return "wechat/subject/qianhua";
	}

	/**
	 * 其他试题
	 */
	@RequestMapping("/drivertype")
	public String Subject(String drivertype,String special,String id,String infotype) {
		getReq().setAttribute("drivertype", drivertype);
		getReq().setAttribute("special", SpecialConvert.SpecialConvertInfo(special));
		getReq().setAttribute("id", id);
		getReq().setAttribute("infotype", infotype);
		return "wechat/subject/drivertype";
	}

	@RequestMapping("/subjectinfo2")
	@ResponseBody
	public void SubjectInfo2(String exam,String examtype,String infotype,String id) {
		WXResult wxResult = new WXResult();
		wxResult.setStatus("0");
		try {
			List<subinfo> list = null;
			Data data = new Data();
			if(infotype.equals("free")) {//免费试题
				list = subInfoServiceImpl.getSubjectInfo("free");
				data.setTitle("免费试题");
			}
			else if(infotype.equals("zhangjie")) {//章节试题
				subchapterBean chapter = _SubChapterServiceImpl.queryById(id);
				list = subInfoServiceImpl.getZhangjieSubject(id);
				data.setTitle(chapter.getName());
			}
			else if(infotype.equals("shunxu")) {//顺序试题
				list = subInfoServiceImpl.getShunXuSubject(exam,examtype);
				data.setTitle(exam+"|"+examtype+"|顺序训练");
			}
			else if(infotype.equals("zhuanxiang")) {//专项训练
				SpecialBean special = _SubSpecialServiceImpl.queryById(id);
				list = subInfoServiceImpl.getZhuanXiang(id);
				data.setTitle(exam+"|"+special.getName());
			}
			else if(infotype.equals("qianhua")) {//强化训练
				data.setTitle(exam+"|"+examtype+"必考套题");
				list = subInfoServiceImpl.getSubjectInfo(id);
			}
			else if(infotype.equals("kaoshi")) {
				//examtype = SpecialConvert.SpecialConvertInfo(examtype);
				List<subinfo> temList = subInfoServiceImpl.getByDriverAndSpecial(exam, examtype);
				int pand = 60;
				int dans = 40;
				int duos = 0;
				if(examtype.equals("科目四")) {
					pand = 20;
					dans = 20;
					duos = 10;
				}
				if(exam.equals("D-E-F")) {
					if(examtype.equals("科目一")) {
						pand = 20;
						dans=30;
						duos = 0;
					}
					else {
						pand = 20;
						dans=20;
						duos = 10;
					}
				}
				list = new ArrayList<>();
				//获取判断题
				List<subinfo> temSubjectList = getSubinfoByType(temList,"1");
				if(pand > temSubjectList.size()) {
					list.addAll(temSubjectList);
				}
				else {
					temSubjectList = getSubinfoBYCount(temSubjectList,pand);
					list.addAll(temSubjectList);
				}
				temSubjectList = getSubinfoByType(temList,"2");
				if(dans > temSubjectList.size()) {
					list.addAll(temSubjectList);
				}
				else {
					temSubjectList = getSubinfoBYCount(temSubjectList,dans);
					list.addAll(temSubjectList);
				}
				if(duos>0) {
					temSubjectList = getSubinfoByType(temList,"3");
					if(duos > temSubjectList.size()) {
						list.addAll(temSubjectList);
					}
					else {
						temSubjectList = getSubinfoBYCount(temSubjectList,duos);
						list.addAll(temSubjectList);
					}
				}
				data.setTitle(exam+"|"+examtype);
			}
			else if(infotype.equals("error")) {
				user u = (user)getReq().getSession().getAttribute("wxuser");
				examtype = SpecialConvert.SpecialConvertInfo(examtype);
				list = subInfoServiceImpl.getErrorSubinfo(exam, examtype,u.getId());
				data.setTitle("我的错题");
			}
			data.setCount(list.size());
			data.setErr(0);
			data.setFileurl("");
			List<Rec> recList = new ArrayList<>();
			for(int i=0,j=list.size();i<j;i++) {
				Rec rec= new Rec(list.get(i),i+1);
				recList.add(rec);
			}
			data.setRec(recList);
			wxResult.setStatus("1");
			wxResult.setData(data);
		}
		catch(Exception e) {
			wxResult.setStatus("0");
			e.printStackTrace();
		}
		responseJson(wxResult);
	}

	private List<subinfo> getSubinfoByType(List<subinfo> list,String type){
		List<subinfo> temSubjectList = new ArrayList<>();
		for(subinfo sub : list) {
			if(sub.getType().equals(type)) {
				temSubjectList.add(sub);
			}
		}
		return temSubjectList;
	}

	private List<subinfo> getSubinfoBYCount(List<subinfo> list,Integer count){
		List<subinfo> temSubjectList = new ArrayList<>();
		Integer all = list.size();
		Random r = new Random();
		while(count>0) {
			int i = r.nextInt(all);
			subinfo sub = list.get(i);
			if(temSubjectList.contains(sub))continue;
			temSubjectList.add(sub);
			count--;
		}
		return temSubjectList;
	}
}
